﻿using System;
using System.IO;
using System.Text;
using System.Xml;
using Microsoft.XmlDiffPatch;
using NUnit.Framework;
using SharpSvn;
using SvnFlow;

namespace SvnFlowTests
{
    public class TestHelper
    {
        public const string Rep1DumpName = "Dumps/rep1.dump";
        public const string Rep2DumpName = "Dumps/rep2.dump";

        public static readonly string m_repo1Path = Path.GetFullPath("repo1") + "/";
        public static readonly string m_repo2Path = Path.GetFullPath("repo2") + "/";

        public static readonly string m_work1Path = Path.GetFullPath("work1");
        public static readonly string m_work2Path = Path.GetFullPath("work2");

        public BranchInfo branchFrom = new BranchInfo(new Uri(m_repo1Path), "from", m_work1Path, new SvnRevisionRange(SvnRevision.Zero, SvnRevision.Head)) { WorkingCopyRevision = SvnRevision.Zero.Revision };
        public BranchInfo branchTo = new BranchInfo(new Uri(m_repo2Path), "to", m_work2Path, new SvnRevisionRange(SvnRevision.Zero, SvnRevision.Head)) { WorkingCopyRevision = SvnRevision.Zero.Revision };

        public static void DeleteRepository(string repositoryPath)
        {
            var rc = new SvnRepositoryClient();
            rc.DeleteRepository(repositoryPath);
        }

        public static void ReCreateRepositoryFromDump(string repositoryPath, string dumpFileName)
        {
            var rc = new SvnRepositoryClient();

            if (Directory.Exists(repositoryPath))
                rc.DeleteRepository(repositoryPath);
            rc.CreateRepository(repositoryPath);

            using (var fs = File.OpenRead(Path.GetFullPath(dumpFileName)))
                rc.LoadRepository(repositoryPath, fs);
        }

        public static void CreateEmptyRepository(string repositoryPath)
        {
            var rc = new SvnRepositoryClient();

            if (Directory.Exists(repositoryPath))
            {
                rc.DeleteRepository(repositoryPath);
            }
            rc.CreateRepository(repositoryPath);
        }


        // String Constants
        public const string EmptyBranchesXml =
@"<?xml version=""1.0""?>
<Branches>
  <Left Uri="""" />
  <Right Uri="""" />

  <MergeSvnProperties>svn:executable,svn:mime-type,svn:ignore,svn:keywords,svn:eol-style,svn:special,svn:needs-lock</MergeSvnProperties>

  <!-- Remove this element if mapping is not required -->
  <MappedFolders>
    <FolderMap Left="""" Right="""" /> 
  </MappedFolders>

  <!-- Specify current revisions in Left and Right folders -->
  <History>
    <HistoryItem Time=""2010-11-09T22:00:00.000"" FromBranch=""left"" LastMergedRevision=""0"" />
    <HistoryItem Time=""2010-11-09T22:00:00.000"" FromBranch=""right"" LastMergedRevision=""0"" />
  </History>
</Branches>";

        public const string Error1BranchesXml =
@"<?xml version=""1.0""?>
<Branches>
  <Left Uri="""" />
</Branches>";
        public const string Error1BranchesXmlMessage = @"Uri in branch left is null or empty
Parameter name: left";

        public const string Error2BranchesXml = 
@"<?xml version=""1.0""?>
<Branches>
  <Right Uri="""" />
</Branches>";

        public const string Error2BranchesXmlMessage = @"Branch left is missing.
Parameter name: left";

        public const string Error3BranchesXml =
@"<?xml version=""1.0""?>
<Branches>
  <Left Uri=""11111"" />
  <Right Uri=""22222"" />
  <History />
</Branches>";

        public const string Error4BranchesXml =
@"<?xml version=""1.0""?>
<Branches>
  <Left Uri=""11111"" />
  <Right Uri=""22222"" />
  <History>
    <HistoryItem Time=""2010-10-09T11:00:00.000"" FromBranch=""left"" LastMergedRevision=""1"" />
  </History>
</Branches>";

        public const string HistoryBranchesXml =
@"<?xml version=""1.0""?>
<Branches>
  <Left Uri=""11111"" />
  <Right Uri=""22222"" />
  <History>
    <HistoryItem Time=""2010-10-09T11:00:00.000"" FromBranch=""left"" LastMergedRevision=""1"" />
  </History>
</Branches>";

        public const string FolderMapBranchesXml =
@"<?xml version=""1.0""?>
<Branches>
  <Left Uri=""11111"" />
  <Right Uri=""22222"" />
  <History/>
  <MappedFolders>
    <FolderMap Left=""1"" Right=""2"" />
    <FolderMap Left=""3"" Right=""4"" />
  </MappedFolders>
</Branches>";

        public const string RepoLog1 =
@"User1|2010.11.08 19:34:42|2|First Commit from User1
User1|2010.11.08 19:29:00|1|
|2010.11.08 19:27:05|0|
";

        public const string RepoLog2 =
@"User1|2010.11.08 20:23:27|2|Second Commit from User1
User1|2010.11.08 19:29:09|1|
|2010.11.08 19:27:09|0|
";

        public static void EditFile(string fileName, string text, string replace)
        {
            File.WriteAllText(fileName, File.ReadAllText(fileName).Replace(text, replace));
        }

        public static void AssertXmlFileEquals(string expectedXml, string actualFileName)
        {
            var actualDoc = new XmlDocument();
            actualDoc.Load(actualFileName);

            var output = new StringBuilder();
            var diff = XmlWriter.Create(output, new XmlWriterSettings { Indent = true });
            var isEqual = new XmlDiff().Compare(new XmlDocument { InnerXml = expectedXml }, actualDoc, diff);
            Assert.That(isEqual, Is.True, output.ToString());
        }
    }
}
